import javax.media.opengl.GL;

import com.sun.opengl.util.texture.Texture;
/*
 * Tomer Nuni 300585312
 * Idan Meshita 300043726
 */

public class MapBuilder {
	
	public Texture wallTexture;
	public Texture cellingTexture;
	public Texture floorTexture;
	private int []rooms;
	
	public MapBuilder() {
		rooms = new int[4];
	}
	
	
	public void buildMap(GL gl){
		double doors [] ={0,0,500,800,0,0,0,0};
		double doors1 [] ={0,0,500,800,0,0,800,500};
		double doors2 [] ={0,0,0,0,0,0,800,500};
		rooms[0]=make_room(gl,1800,500,1800,doors,wallTexture,floorTexture,cellingTexture);
		rooms[1]=make_room(gl,1800,500,1800,doors1,wallTexture,floorTexture,cellingTexture);
		rooms[2]=make_room(gl,1800,500,1800,doors2,wallTexture,floorTexture,cellingTexture);
	}
	

 private int make_room(GL gl,double sizeX,double sizeY,double sizeZ,double[] doors,Texture wallTexture,Texture floorTexture,Texture cellingTexture){

		int room = gl.glGenLists(1);

		gl.glNewList(room, GL.GL_COMPILE);

        gl.glTexParameteri ( GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT );
        gl.glTexParameteri( GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT );
//        gl.glTexParameteri( GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP );

 		//walls
        wallTexture.bind();
		gl.glBegin(GL.GL_QUAD_STRIP);

        
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3d(0, 0, 0);
		gl.glTexCoord2f(0.0f,4.0f);
		gl.glVertex3d(0, sizeY, 0);
		
		
		gl.glTexCoord2f((float)(8/(sizeX/doors[0])), 0.0f);
		gl.glVertex3d(doors[0], 0, 0);
		
		gl.glTexCoord2f((float)(8/(sizeX/doors[0])), 4f);
		gl.glVertex3d(doors[0], sizeY, 0);
		
		gl.glEnd();
		gl.glBegin(GL.GL_QUAD_STRIP);

		gl.glTexCoord2f(0f, 0.0f);
		gl.glVertex3d(doors[1], 0, 0);
		gl.glTexCoord2f(0f, 4f);
		gl.glVertex3d(doors[1], sizeY, 0);

		gl.glTexCoord2f(8f, 0.0f);
		gl.glVertex3d(sizeX, 0, 0);
		gl.glTexCoord2f(8f, 4f);
		gl.glVertex3d(sizeX, sizeY, 0);

		
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3d(sizeX, 0, doors[2]);
		gl.glTexCoord2f(0.0f,4.0f);
		gl.glVertex3d(sizeX, sizeY, doors[2]);
		
		gl.glEnd();
		gl.glBegin(GL.GL_QUAD_STRIP);
		
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3d(sizeX, 0, doors[3]);
		gl.glTexCoord2f(0.0f,4.0f);
		gl.glVertex3d(sizeX, sizeY, doors[3]);
		
		
		gl.glTexCoord2f(8f, 0.0f);
		gl.glVertex3d(sizeX, 0, sizeZ);
		gl.glTexCoord2f(8f,4.0f);
		gl.glVertex3d(sizeX, sizeY, sizeZ);
		
	
		
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3d(doors[4], 0, sizeZ);
		gl.glTexCoord2f(0.0f,4.0f);
		gl.glVertex3d(doors[4], sizeY, sizeZ);
		
		gl.glEnd();
		gl.glBegin(GL.GL_QUAD_STRIP);
		
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3d(doors[5], 0, sizeZ);
		gl.glTexCoord2f(0.0f,4.0f);
		gl.glVertex3d(doors[5], sizeY, sizeZ);
		
	
		gl.glTexCoord2f(8f, 0.0f);
		gl.glVertex3d(0, 0, sizeZ);
		gl.glTexCoord2f(8f, 4f);
		gl.glVertex3d(0, sizeY, sizeZ);
///
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3d(0, 0, doors[6]);
		gl.glTexCoord2f(0.0f,4.0f);
		gl.glVertex3d(0, sizeY, doors[6]);
		
		gl.glEnd();
		gl.glBegin(GL.GL_QUAD_STRIP);
		
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3d(0, 0, doors[7]);
		gl.glTexCoord2f(0.0f,4.0f);
		gl.glVertex3d(0, sizeY, doors[7]);
		

		///
		gl.glTexCoord2f(8f, 0.0f);
		gl.glVertex3d(0, 0, 0);
		gl.glTexCoord2f(8f,4.0f);
		gl.glVertex3d(0, sizeY, 0);
	    gl.glEnd();
	    
        gl.glTexParameteri ( GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT );
        gl.glTexParameteri( GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT );
        
        // build floor
        floorTexture.bind();
        gl.glBegin(GL.GL_QUAD_STRIP);
		
        gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3d(0,0 , 0);
		gl.glTexCoord2f(0.0f,8.0f);
		gl.glVertex3d(0, 0, sizeZ);
		gl.glTexCoord2f(4.0f, 0.0f);
		gl.glVertex3d(sizeX, 0, 0);
		gl.glTexCoord2f(4.0f,8f);
		gl.glVertex3d(sizeX, 0, sizeZ);
		gl.glEnd();
	    gl.glTexParameteri ( GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT );
	    gl.glTexParameteri( GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT );
		
	    // build celling
	    cellingTexture.bind();
	    gl.glBegin(GL.GL_QUAD_STRIP);
        gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3d(0, sizeY, 0);
		gl.glTexCoord2f(0.0f,4f);
		gl.glVertex3d(0, sizeY, sizeZ);
		gl.glTexCoord2f(8f, 0.0f);
		gl.glVertex3d(sizeX, sizeY, 0);
		gl.glTexCoord2f(8f,4f);
		gl.glVertex3d(sizeX, sizeY, sizeZ);
		gl.glEnd();


	    gl.glEndList();
	    return room;
	}
 
	public void draw(GL gl) {
		
		
		
		gl.glCallList(rooms[0]);
		
		gl.glPushMatrix();
		gl.glTranslated(1800, 0, 0);
		gl.glCallList(rooms[1]);
		gl.glPopMatrix();
		
		gl.glPushMatrix();
		gl.glTranslated(3600, 0, 0);
		gl.glCallList(rooms[2]);
		gl.glPopMatrix();
		
		
	}
}
